<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tomcat多应用隔离技术指南</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        .title-font {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-bg {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .code-block {
            background-color: #2d3748;
            color: #f7fafc;
            border-radius: 0.375rem;
            font-family: monospace;
        }
        .highlight {
            position: relative;
        }
        .highlight:before {
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            width: 4px;
            background-color: #4299e1;
            border-radius: 0.375rem 0 0 0.375rem;
        }
        .nav-link {
            position: relative;
        }
        .nav-link:after {
            content: "";
            position: absolute;
            left: 0;
            bottom: -2px;
            width: 0;
            height: 2px;
            background-color: #4299e1;
            transition: width 0.3s ease;
        }
        .nav-link:hover:after {
            width: 100%;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-bg text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="title-font text-4xl md:text-5xl font-bold mb-6">Tomcat多应用隔离技术</h1>
                    <p class="text-xl mb-8 opacity-90">构建安全稳定的Web应用部署环境</p>
                    <div class="flex space-x-4">
                        <a href="#content" class="bg-white text-blue-800 px-6 py-3 rounded-lg font-medium hover:bg-gray-100 transition duration-300">
                            <i class="fas fa-book-open mr-2"></i>开始阅读
                        </a>
                        <a href="#summary" class="border border-white text-white px-6 py-3 rounded-lg font-medium hover:bg-white hover:text-blue-800 transition duration-300">
                            <i class="fas fa-chart-pie mr-2"></i>技术概览
                        </a>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="bg-white bg-opacity-20 p-8 rounded-xl backdrop-filter backdrop-blur-sm">
                        <div class="mermaid">
                            graph TD
                                A[Tomcat多应用隔离] --> B[应用级别隔离]
                                A --> C[虚拟主机级别隔离]
                                A --> D[实例级别隔离]
                                B --> B1[独立的Context配置]
                                B --> B2[ClassLoader隔离]
                                C --> C1[虚拟主机配置]
                                D --> D1[多实例部署]
                                D --> D2[Docker容器]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Navigation -->
    <nav class="bg-white shadow-sm sticky top-0 z-10">
        <div class="container mx-auto max-w-5xl">
            <div class="flex justify-center space-x-8 py-4">
                <a href="#app-level" class="nav-link text-gray-700 font-medium">应用隔离</a>
                <a href="#virtual-host" class="nav-link text-gray-700 font-medium">虚拟主机</a>
                <a href="#instance-level" class="nav-link text-gray-700 font-medium">实例隔离</a>
                <a href="#comparison" class="nav-link text-gray-700 font-medium">方案对比</a>
            </div>
        </div>
    </nav>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl py-12 px-4 md:px-0" id="content">
        <!-- Summary Section -->
        <section class="mb-20" id="summary">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <h2 class="title-font text-3xl font-bold mb-6 text-gray-800">技术概览</h2>
                    <p class="text-gray-700 mb-8">在Tomcat中实现多应用隔离是确保不同应用不相互干扰、提高系统安全性和稳定性的关键技术。本文详细介绍三种不同级别的隔离方案及其实现方法。</p>
                    
                    <div class="grid md:grid-cols-3 gap-6">
                        <div class="bg-blue-50 p-6 rounded-lg border border-blue-100 card-hover">
                            <div class="text-blue-600 mb-4">
                                <i class="fas fa-layer-group text-3xl"></i>
                            </div>
                            <h3 class="title-font text-xl font-semibold mb-3 text-gray-800">应用级别隔离</h3>
                            <p class="text-gray-600">通过独立的Context配置和ClassLoader机制实现应用间的隔离，适合部署多个相关应用。</p>
                        </div>
                        <div class="bg-purple-50 p-6 rounded-lg border border-purple-100 card-hover">
                            <div class="text-purple-600 mb-4">
                                <i class="fas fa-server text-3xl"></i>
                            </div>
                            <h3 class="title-font text-xl font-semibold mb-3 text-gray-800">虚拟主机隔离</h3>
                            <p class="text-gray-600">配置多个虚拟主机，实现完全隔离的应用环境，适合为不同客户或业务提供服务。</p>
                        </div>
                        <div class="bg-green-50 p-6 rounded-lg border border-green-100 card-hover">
                            <div class="text-green-600 mb-4">
                                <i class="fas fa-cubes text-3xl"></i>
                            </div>
                            <h3 class="title-font text-xl font-semibold mb-3 text-gray-800">实例级别隔离</h3>
                            <p class="text-gray-600">通过多个Tomcat实例或Docker容器实现最高级别的隔离，适合关键业务系统。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- App Level Isolation -->
        <section class="mb-20" id="app-level">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="bg-gray-800 text-white px-8 py-4">
                    <h2 class="title-font text-2xl font-semibold flex items-center">
                        <i class="fas fa-layer-group mr-3"></i> 1. 应用级别的隔离
                    </h2>
                </div>
                <div class="p-8">
                    <h3 class="title-font text-xl font-semibold mb-6 text-gray-800 flex items-center">
                        <span class="bg-blue-100 text-blue-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">1.1</span>
                        使用独立的Context
                    </h3>
                    <p class="text-gray-700 mb-6">Tomcat的<code class="bg-gray-100 px-1 py-0.5 rounded">Context</code>元素代表一个Web应用。每个Web应用都可以在<code class="bg-gray-100 px-1 py-0.5 rounded">server.xml</code>或<code class="bg-gray-100 px-1 py-0.5 rounded">context.xml</code>文件中定义一个独立的<code class="bg-gray-100 px-1 py-0.5 rounded">Context</code>，这允许应用在不同的虚拟主机上运行，或者在相同的虚拟主机下但有不同的路径。</p>
                    
                    <div class="mb-6 highlight">
                        <div class="code-block p-4 overflow-x-auto">
                            <pre><code>&lt;Context path="/app1" docBase="/path/to/app1" /&gt;
&lt;Context path="/app2" docBase="/path/to/app2" /&gt;</code></pre>
                        </div>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6 mb-8">
                        <div>
                            <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                                <i class="fas fa-info-circle text-blue-500 mr-2"></i>配置说明
                            </h4>
                            <ul class="text-gray-700 space-y-2">
                                <li><code class="bg-gray-100 px-1 py-0.5 rounded">path</code>：应用的访问路径</li>
                                <li><code class="bg-gray-100 px-1 py-0.5 rounded">docBase</code>：应用的实际路径</li>
                            </ul>
                        </div>
                        <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4">
                            <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                                <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>最佳实践
                            </h4>
                            <p class="text-gray-700">对于生产环境，建议将Context配置放在独立的<code class="bg-gray-100 px-1 py-0.5 rounded">context.xml</code>文件中，而不是直接修改<code class="bg-gray-100 px-1 py-0.5 rounded">server.xml</code>。</p>
                        </div>
                    </div>
                    
                    <h3 class="title-font text-xl font-semibold mb-6 text-gray-800 flex items-center mt-12">
                        <span class="bg-blue-100 text-blue-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">1.2</span>
                        配置Web应用的ClassLoader
                    </h3>
                    <p class="text-gray-700 mb-6">Tomcat提供了多种ClassLoader配置，允许为不同的Web应用配置不同的ClassLoader，以实现类的隔离。</p>
                    
                    <div class="mb-6 highlight">
                        <div class="code-block p-4 overflow-x-auto">
                            <pre><code>&lt;Context&gt;
    &lt;Loader className="org.apache.catalina.loader.WebappLoader" delegate="false"/&gt;
&lt;/Context&gt;</code></pre>
                        </div>
                    </div>
                    
                    <p class="text-gray-700">
                        <code class="bg-gray-100 px-1 py-0.5 rounded">delegate="false"</code>表示不使用父ClassLoader进行类加载，而是使用当前应用的ClassLoader。
                    </p>
                </div>
            </div>
        </section>

        <!-- Virtual Host Level Isolation -->
        <section class="mb-20" id="virtual-host">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="bg-gray-800 text-white px-8 py-4">
                    <h2 class="title-font text-2xl font-semibold flex items-center">
                        <i class="fas fa-server mr-3"></i> 2. 虚拟主机级别的隔离
                    </h2>
                </div>
                <div class="p-8">
                    <h3 class="title-font text-xl font-semibold mb-6 text-gray-800 flex items-center">
                        <span class="bg-purple-100 text-purple-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">2.1</span>
                        使用虚拟主机（Virtual Hosts）
                    </h3>
                    <p class="text-gray-700 mb-6">通过配置多个虚拟主机，可以将不同的应用部署在不同的虚拟主机上，虚拟主机之间完全隔离。</p>
                    
                    <div class="mb-6 highlight">
                        <div class="code-block p-4 overflow-x-auto">
                            <pre><code>&lt;Host name="www.example1.com" appBase="webapps/example1" unpackWARs="true" autoDeploy="true"&gt;
    &lt;Context path="" docBase="/path/to/example1" /&gt;
&lt;/Host&gt;

&lt;Host name="www.example2.com" appBase="webapps/example2" unpackWARs="true" autoDeploy="true"&gt;
    &lt;Context path="" docBase="/path/to/example2" /&gt;
&lt;/Host&gt;</code></pre>
                        </div>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div>
                            <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                                <i class="fas fa-info-circle text-purple-500 mr-2"></i>配置说明
                            </h4>
                            <ul class="text-gray-700 space-y-2">
                                <li><code class="bg-gray-100 px-1 py-0.5 rounded">name</code>：虚拟主机的域名</li>
                                <li><code class="bg-gray-100 px-1 py-0.5 rounded">appBase</code>：虚拟主机的应用基础目录</li>
                            </ul>
                        </div>
                        <div class="bg-blue-50 border-l-4 border-blue-400 p-4">
                            <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                                <i class="fas fa-cog text-blue-500 mr-2"></i>配置要点
                            </h4>
                            <p class="text-gray-700">确保DNS配置正确，将不同域名解析到Tomcat服务器的IP地址。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Instance Level Isolation -->
        <section class="mb-20" id="instance-level">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="bg-gray-800 text-white px-8 py-4">
                    <h2 class="title-font text-2xl font-semibold flex items-center">
                        <i class="fas fa-cubes mr-3"></i> 3. 实例级别的隔离
                    </h2>
                </div>
                <div class="p-8">
                    <h3 class="title-font text-xl font-semibold mb-6 text-gray-800 flex items-center">
                        <span class="bg-green-100 text-green-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">3.1</span>
                        使用多个Tomcat实例
                    </h3>
                    <p class="text-gray-700 mb-6">部署多个Tomcat实例在不同的端口上，每个实例都有独立的<code class="bg-gray-100 px-1 py-0.5 rounded">conf</code>、<code class="bg-gray-100 px-1 py-0.5 rounded">webapps</code>、<code class="bg-gray-100 px-1 py-0.5 rounded">logs</code>等目录，这样可以完全隔离不同实例之间的应用和配置。</p>
                    
                    <div class="mb-6 highlight">
                        <div class="code-block p-4 overflow-x-auto">
                            <pre><code>&lt;Connector port="8080" protocol="HTTP/1.1" ... /&gt;</code></pre>
                        </div>
                    </div>
                    
                    <p class="text-gray-700 mb-8">可以为每个Tomcat实例配置不同的端口号和应用路径。</p>
                    
                    <h3 class="title-font text-xl font-semibold mb-6 text-gray-800 flex items-center mt-12">
                        <span class="bg-green-100 text-green-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">3.2</span>
                        使用Docker容器隔离
                    </h3>
                    <p class="text-gray-700 mb-6">将Tomcat部署在Docker容器中，可以实现更高的隔离性。每个Tomcat实例运行在独立的Docker容器中，相互之间完全隔离。</p>
                    
                    <div class="mb-6 highlight">
                        <div class="code-block p-4 overflow-x-auto">
                            <pre><code>FROM tomcat:9.0
COPY ./app1.war /usr/local/tomcat/webapps/</code></pre>
                        </div>
                    </div>
                    
                    <p class="text-gray-700">
                        <code class="bg-gray-100 px-1 py-0.5 rounded">app1.war</code>文件被拷贝到Tomcat容器的<code class="bg-gray-100 px-1 py-0.5 rounded">webapps</code>目录下，应用在容器中独立运行。
                    </p>
                    
                    <div class="grid md:grid-cols-2 gap-6 mt-8">
                        <div>
                            <h3 class="title-font text-xl font-semibold mb-6 text-gray-800 flex items-center">
                                <span class="bg-green-100 text-green-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">3.3</span>
                                配置不同的server.xml
                            </h3>
                            <p class="text-gray-700 mb-6">每个Tomcat实例都有独立的<code class="bg-gray-100 px-1 py-0.5 rounded">server.xml</code>配置文件，可以设置不同的端口、虚拟主机和应用路径。</p>
                            
                            <div class="mb-6">
                                <div class="code-block p-4 overflow-x-auto">
                                    <pre><code>&lt;Connector port="8080" ... /&gt;</code></pre>
                                </div>
                                <p class="text-gray-700 text-sm mt-2">第一个Tomcat实例（端口8080）</p>
                            </div>
                            
                            <div class="mb-6">
                                <div class="code-block p-4 overflow-x-auto">
                                    <pre><code>&lt;Connector port="8081" ... /&gt;</code></pre>
                                </div>
                                <p class="text-gray-700 text-sm mt-2">第二个Tomcat实例（端口8081）</p>
                            </div>
                        </div>
                        <div>
                            <h3 class="title-font text-xl font-semibold mb-6 text-gray-800 flex items-center">
                                <span class="bg-green-100 text-green-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">3.4</span>
                                使用不同的目录结构
                            </h3>
                            <p class="text-gray-700 mb-6">每个Tomcat实例应有独立的<code class="bg-gray-100 px-1 py-0.5 rounded">conf</code>、<code class="bg-gray-100 px-1 py-0.5 rounded">webapps</code>和<code class="bg-gray-100 px-1 py-0.5 rounded">logs</code>目录，以确保不同实例的配置、应用和日志文件不会互相影响。</p>
                            
                            <div class="mb-6">
                                <div class="code-block p-4 overflow-x-auto">
                                    <pre><code>tomcat-instance-1/
    conf/
    webapps/
    logs/

tomcat-instance-2/
    conf/
    webapps/
    logs/</code></pre>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-20" id="comparison">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="bg-gray-800 text-white px-8 py-4">
                    <h2 class="title-font text-2xl font-semibold flex items-center">
                        <i class="fas fa-balance-scale mr-3"></i> 隔离方案对比
                    </h2>
                </div>
                <div class="p-8">
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">方案</th>
                                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">隔离级别</th>
                                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">配置复杂度</th>
                                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">资源消耗</th>
                                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">适用场景</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                <tr class="hover:bg-gray-50">
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">应用级别隔离</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">中</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">简单</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">低</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">开发环境、小型应用</td>
                                </tr>
                                <tr class="hover:bg-gray-50">
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">虚拟主机隔离</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">高</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">中等</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">中</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">多租户环境、不同业务线</td>
                                </tr>
                                <tr class="hover:bg-gray-50">
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">实例级别隔离</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">最高</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">复杂</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">高</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">生产环境、关键业务系统</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-8 bg-blue-50 p-6 rounded-lg border border-blue-200">
                        <h3 class="title-font text-xl font-semibold mb-4 text-gray-800 flex items-center">
                            <i class="fas fa-lightbulb text-blue-500 mr-2"></i> 选择建议
                        </h3>
                        <p class="text-gray-700 mb-4">根据业务需求选择合适的隔离方案：</p>
                        <ul class="list-disc pl-6 text-gray-700 space-y-2">
                            <li>开发测试环境：应用级别隔离通常足够</li>
                            <li>多客户/多业务环境：虚拟主机隔离更为合适</li>
                            <li>高安全要求的生产环境：实例级别隔离或Docker容器是最佳选择</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-medium text-white">技术小馆</h3>
                    <p class="text-sm mt-1">专业的技术知识分享平台</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-300">
                        <i class="fas fa-external-link-alt mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-sm text-center text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Smooth scrolling for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>